package com.echo.code.thread.mode.listener;

import java.util.concurrent.ExecutorService;

/**
 * EventBus Created by baiyap on 2018/9/24.
 */
public class EventBus implements Bus {

    private final Registry registry;
    private final Dispatcher dispatcher;

    public EventBus(ExecutorService executorService, EventExceptionHandler exceptionHandler) {
        registry = new Registry();
        dispatcher = new Dispatcher(executorService, exceptionHandler);
    }

    @Override
    public void register(Object subscriber) {
        registry.bind(subscriber);
    }

    @Override
    public void unregister(Object subscriber) {
        registry.unbind(subscriber);
    }

    @Override
    public void post(Object event, String topic) {
        dispatcher.dispatch(registry, event, topic);
    }

    @Override
    public void close() {
        dispatcher.close();
    }
}